/* main.c */

#include <stdio.h>

#include "board.h"

#include "sdspi.h"

extern const sdspi_interface_t sdspi_if;
sdspi_card_t sdspi_card;
uint8_t sdspi_read_buff[SDSPI_DEFAULT_BLOCK_SIZE * 2u];
uint8_t sdspi_write_buff[SDSPI_DEFAULT_BLOCK_SIZE * 2u];

void app_sdspi_read_test(void);
void app_sdspi_write_test(void);

int main(void)
{
    board_init();
    printf("sdspi example.\r\n");

    status_t err = SDSPI_Init(&sdspi_card, &sdspi_if);
    if (kStatus_Success != err)
    {
        printf("SDSPI_Init() failed.\r\n");
        while (1);
    }
    printf("SDSPI_Init() done.\r\n");
    SDSPI_SwitchToHighSpeed(&sdspi_card);

    while (1)
    {
        printf("press anykey to read ...\r\n");
        getchar();
        app_sdspi_read_test();

        printf("press any key to write ...\r\n");
        getchar();
        app_sdspi_write_test();
    }
}

void app_sdspi_read_test(void)
{
    status_t err;

    err = SDSPI_ReadBlocks(&sdspi_card, sdspi_read_buff, 3u, 2u); /* block 3 and block 4. */
    if (kStatus_Success != err)
    {
        printf("SDSPI_ReadBlocks() failed.\r\n");
    }
    else
    {
        printf("SDSPI_ReadBlocks() done.\r\n");
        for (uint32_t i = 0u; i < 1024; i++)
        {
            if ((i % 16) == 0u )
            {
                printf("\r\n");
            }
            printf("0x%2x ",  sdspi_read_buff[i]);
        }
        printf("\r\n");
    }
}

void app_sdspi_write_test(void)
{
    status_t err;

    for (uint32_t i = 0u; i < SDSPI_DEFAULT_BLOCK_SIZE*2u; i++)
    {
        sdspi_write_buff[i] = sdspi_read_buff[i]+1u;
    }
    err = SDSPI_WriteBlocks(&sdspi_card, sdspi_write_buff, 3u, 2u); /* block 3 and block 4. */
    if (kStatus_Success != err)
    {
        printf("SDSPI_WriteBlocks() failed.\r\n");
    }
    else
    {
        printf("SDSPI_WriteBlocks() done.\r\n");
    }
}


/* EOF. */

